mod global;
mod logger_thread;
mod message;
mod thread_local;
pub use self::global::LOGGER_GLOBAL;
pub use self::message::Message;

use crate::TargetMapping;

#[cfg(feature = "in-async")]
mod task_local;
#[cfg(feature = "in-async")]
pub fn record_log<T: TargetMapping>(record: &log::Record) {
    let record_log_with_task = |local: &task_local::LoggerTaskLocal| {
        if let Err(err) = local.record_log::<T>(record) {
            println!(
                "record task log error:{}, line:{:?} file:{:?}: level:{} msg:{}",
                err,
                record.line(),
                record.file_static(),
                record.level(),
                record.args()
            );
        }
    };

    let record_log_with_thread = |local: &thread_local::LoggerLocal| {
        if let Err(err) = local.record_log::<T>(record) {
            println!(
                "record thread log error:{}, line:{:?} file:{:?}: level:{} msg:{}",
                err,
                record.line(),
                record.file_static(),
                record.level(),
                record.args()
            );
        }
    };

    if task_local::try_with_task_local(record_log_with_task).is_err() {
        thread_local::with_thread_local(record_log_with_thread)
    }
}

#[cfg(not(feature = "in-async"))]
pub fn record_log<T: TargetMapping>(record: &log::Record) {
    let record_log = |local: &thread_local::LoggerLocal| {
        if let Err(err) = local.record_log::<T>(record) {
            println!(
                "record thread log error:{}, line:{:?} file:{:?}: level:{} msg:{}",
                err,
                record.line(),
                record.file_static(),
                record.level(),
                record.args()
            );
        }
    };

    thread_local::with_thread_local(record_log)
}
